﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace MTHHelpers
{
    public class IniConfigHelper
    {

        public static string iniPath = string.Empty;

        #region API函数声明

        //[DllImport("kernel32")]//返回0表示失败，非0为成功
        //private static extern long WritePrivateProfileString(string section, string key,
        //    string val, string filePath);

        //[DllImport("kernel32")]//返回取得字符串缓冲区的长度
        //private static extern long GetPrivateProfileString(string section, string key,
        //    string def, StringBuilder retVal, int size, string filePath);

        /// <summary>
        /// 为INI文件中指定的节点取得字符串
        /// </summary>
        /// <param name="lpAppName">欲在其中查找关键字的节点名称</param>
        /// <param name="lpKeyName">欲获取的项名</param>
        /// <param name="lpDefault">指定的项没有找到时返回的默认值</param>
        /// <param name="lpReturnedString">指定一个字串缓冲区，长度至少为nSize</param>
        /// <param name="nSize">指定装载到lpReturnedString缓冲区的最大字符数量</param>
        /// <param name="lpFileName">INI文件名</param>
        /// <returns>复制到lpReturnedString缓冲区的字节数量，其中不包括那些NULL中止字符</returns>
        [DllImport("kernel32")]
        public static extern int GetPrivateProfileString(
            string lpSectionName,
            string lpKeyName,
            string lpDefault,
            StringBuilder lpReturnedString,
            int nSize,
            string lpFileName);

        /// <summary>
        /// 修改INI文件中内容
        /// </summary>
        /// <param name="lpApplicationName">欲在其中写入的节点名称</param>
        /// <param name="lpKeyName">欲设置的项名</param>
        /// <param name="lpString">要写入的新字符串</param>
        /// <param name="lpFileName">INI文件名</param>
        /// <returns>非零表示成功，零表示失败</returns>
        [DllImport("kernel32")]
        public static extern int WritePrivateProfileString(
            string lpSectionName,
            string lpKeyName,
            string lpString,
            string lpFileName);
        #endregion

        #region 读Ini文件

        public string IniFilePath { get; set; }
        public IniConfigHelper(string path)
        {
            IniFilePath = path;
        }
        public static string ReadIniData(string Section, string Key, string NoText,string path)
        {
            if (File.Exists(path))
            {
                StringBuilder temp = new StringBuilder(1024);
                GetPrivateProfileString(Section, Key, NoText, temp, 1024, path);
                return temp.ToString();
            }
            else
            {
                return String.Empty;
            }
        }

        #endregion

        #region 写Ini文件

        public static bool WriteIniData(string Section, string Key, string Value, string Path)
        {

            long OpStation = WritePrivateProfileString(Section, Key, Value, Path);
            if (OpStation == 0)
            {
                return false;
            }
            else
            {
                return true;
            }

        }

        #endregion

    }


}


